<template>
	<view class="main">
		<web-view src="http://192.168.50.145:8083/#/pages/blindBox/index" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		onShow() {

		},
		methods: {
			// 监听页面发送的消息
			sendMessageToH5(data) {
				let currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
				console.log('[currentWebview-------------]', currentWebview.children())
				let wv = currentWebview.children()[0];
				// params 传递的参数
				let params = JSON.stringify(data)
			 // wv.evalJS调用H5方法并传递数据
				wv.evalJS("getUniAppMessage(" + params + ")") //getUniAppMessage h5中获取数据的方法
			},
			handleMessage(e) {
				// 传递数据在 e.detail.data
				let msgData = e.detail.data[0];
				if (msgData.type == 'http') {
					this.http(msgData.name, msgData.data)
				}
				if (msgData.type == 'share') {
					this.WxShare(msgData.data)
				}
				// 以下可判断传来数据，并做处理
				console.log('[接----------]', msgData)
				// this.sendMessageToH5()
			},
			async http(name, params) {
				let res = await this.$http(params)
				console.log('[发送请求---------------]', res)
				console.log('[发送请求---------------]', name)
				if (res.code == 200) {
					this.sendMessageToH5({
						name: name,
						data: res.data
					})
				}
			},
			//邀请好友
			WxShare(data) {
				console.log('-----------[分享]--------------', data)
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.share({
					provider: data.provider,
					scene: data.scene,
					type: data.type,
					href: data.href,
					title: data.title,
					imageUrl: data.imageUrl,
					summary: data.summary,
					success: (res) => {
						console.log("[分享成功]", res);
						uni.showToast({
							title: '发送邀请成功',
							duration: 2000,
							icon: 'none'
						});
					},
					fail: (err) => {
						console.log("[分享失败]", err);
						uni.showToast({
							title: '发送邀请失败' + err.errMsg,
							duration: 2000,
							icon: 'none'
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {}
</style>
